package meijuli.com.join51.business.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jhx.common.utils.Constants;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.CertificationActivity;
import meijuli.com.join51.business.activity.MainActivity;
import meijuli.com.join51.business.model.Status;
import com.jhx.common.model.User;
import meijuli.com.join51.business.utils.StringUtil;
import com.jhx.common.utils.UserManager;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 认证前页面
 *
 * @author wenqin 2017-04-14 10:14
 */

public class ReviewBeforeFragment extends BaseFragment {
    TextView mAccount;
    TextView mPhone;
    CircleImageView mHeadIcon;
    TextView mReviewStatusText;
    TextView mreviewTxt;
    RelativeLayout mAccountStatus;
    RelativeLayout mHeadView;

    public static ReviewBeforeFragment newInstance(User user, Status status) {

        Bundle args = new Bundle();
        args.putSerializable(MainActivity.KEY_USER, user);
        args.putSerializable(MainActivity.KEY_STATUS, status);

        ReviewBeforeFragment fragment = new ReviewBeforeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_review_before, container, false);
        mAccount = view.findViewById(R.id.account);
        mPhone = view.findViewById(R.id.phone);
        mHeadIcon = view.findViewById(R.id.head_icon);
        mReviewStatusText = view.findViewById(R.id.review_status_text);
        mreviewTxt = view.findViewById(R.id.review_txt);
        mAccountStatus = view.findViewById(R.id.rl_account_info);
        mHeadView = view.findViewById(R.id.rl_head);
        mreviewTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toReview();
            }
        });
        return view;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHeadView.setVisibility(View.VISIBLE);
        if (getArguments() != null) {
            User user = (User) getArguments().getSerializable(MainActivity.KEY_USER);
            if (user == null) {
                user = UserManager.get().getUser();
            }
            if (user == null) {
                return;
            } // 有隐患

            mAccount.setText(StringUtil.limitAccounNameLength(user.getBusinessName()));
            mPhone.setText(StringUtil.hiePhoneNum(user.getPhone()));
            Glide.with(this).load(user.getHeadImage())
                    .error(R.mipmap.icon_header)
                    .into(mHeadIcon);
            if (user.getAuthStatus() == Constants.ApprovalStatus.APPROVAL_SUCCESS) {
                mAccountStatus.setVisibility(View.GONE);
            } else {
                mAccountStatus.setVisibility(View.VISIBLE);
            }
        }
    }

    void toReview() {
        CertificationActivity.start(getActivity(), 0);
    }

}
